<?php
namespace My\Controller;
use Utuo\BackgroundController;

class MicroVoteController extends BackgroundController
{   private $selectTypes = array('0'=>'单选','1'=>'多选');
    private $time = array('0'=>'不计时投票','1'=>'计时投票');
	public function _initialize() {
		$this->setControllerTitle("微投票管理");
		$this->setActionTitle(array(
				'index' => '微投票列表',
				'add' => '微投票添加',
				'modify' => '修改微投票',
				'recycle' => '回收站'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
    public function getLists( $is_deleted=0 ) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$formId = I('id', 0, "intval");
		$voteLists = M('MicroVote')->where("cid=%d and is_deleted={$is_deleted}", $this->aid)->limit($pagesize)->page($page)->select();
		foreach ($voteLists as &$v)
		{
			$v['url']="http://".$_SERVER['HTTP_HOST'].U('Home/MicroVote/index',array('sid'=>$this->aid,'id'=>$v['id']));
		}
		$voteCount = M("MicroVote")->where("cid=%d and is_deleted={$is_deleted}", $this->aid)->count();
		
		$this->showPage($voteCount, $pagesize);
		$this->assign("voteLists", $voteLists);
		$this->display();
	}
	
	/**
	 * 添加投票表单
	 */
	public function addAction() {
		C('TOKEN_ON', flase);
		$vote = array();
		$vote['selectOptionsRadio'] = $this->getRadio("select_options", $this->selectTypes, 0);
		$vote['timeOptionsRadio'] = $this->getRadio("time_type", $this->time, 0);
		$this->assign("vote", $vote);
		$this->display('save');
	}
	
	/**
	 * 修改投票表单
	 */
	public function modifyAction() {
		$id = I('id', 0, 'intval');
		$vote = M("MicroVote")->find($id);
		
		$vote['answer'] = M('MicroVoteItem')->where('micro_vote_id=%d', $id)->order("sort")->select();
		$vote['selectOptionsRadio'] = $this->getRadio("select_options", $this->selectTypes, $vote['select_options']);
		$vote['timeOptionsRadio'] = $this->getRadio("time_type", $this->time, $vote['time_type']);
		$this->assign("vote", $vote);
		$this->display('save');
	}
	
	/**
	 * 保存投票表单
	 */
	public function saveAction() {
		false !== D("MicroVote")->submit() and $this->ajaxSuccess("保存成功");
		$this->ajaxError("保存失败".D("MicroVote")->getError());
	}
	// 移除
	public function removeAction() {
		$this->ajaxRemove("MicroVote");
	}
	// 删除
	public function deleteAction() {
		$this->ajaxDelete("MicroVote");
	}
	public function setOpenAction(){
		$open=I('open');
		//M('MicroVote')->where('id=%d',I('id'))->save(array('open'=>$open));
		//echo M()->getLastSql();
		false!==M('MicroVote')->where('id=%d',I('id'))->save(array('open'=>1-$open)) ? $this->ajaxSuccess():$this->ajaxError();
	}
	// 恢复
	public function restoreAction() {
		$this->ajaxRestore("MicroVote");
	}
}